package com.zjitc.controller;

import com.zjitc.model.Country;
import com.zjitc.service.UserService;


import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;



/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/20 16:16
 * @description:
 */
@Controller
public class UserController {
  @Autowired
  private UserService userService;

  @RequestMapping("/findAll")
  public ModelAndView findAll(){

    List<Country> listCountry=userService.findAll();

    ModelAndView mv=new ModelAndView();
    //request.setAttribute(key,value)
    mv.addObject("country",listCountry);
    mv.setViewName("/views/country.jsp");
    return mv;
  }

  @RequestMapping("/insert")
  public ModelAndView insert(Country country)  {
    userService.insert(country);
    return new ModelAndView("redirect:/findAll.do");
  }

  @RequestMapping("/delete")
  public ModelAndView delete(HttpServletRequest request) throws IOException {

    String code = request.getParameter("code");
    userService.delete(code);
    return new ModelAndView("redirect:/findAll.do");
  }

  @RequestMapping("/search")
  public ModelAndView search(Country country,HttpServletRequest request){
    String condition = request.getParameter("condition");
    List<Country> list=userService.search(condition);

    ModelAndView mv=new ModelAndView();
    mv.addObject("country",list);
    mv.setViewName("/views/country.jsp");
    return mv;
  }

  @RequestMapping("/updateUI")
  public ModelAndView updateUI(HttpServletRequest request){
    String code=request.getParameter("code");
    Country country= userService.findCode(code);
    ModelAndView mv=new ModelAndView();
    mv.addObject("p",country);
    mv.setViewName("/views/countryUpdate.jsp");
    return mv;
  }

  @RequestMapping("/update")
  public ModelAndView update(Country country){
    ModelAndView mv=new ModelAndView();
    int update = userService.update(country);
    return new ModelAndView("redirect:/findAll.do");
  }
}
